package com.my.xl.salary.billdemo.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.my.xl.salary.billdemo.dao.BillDao;
import com.my.xl.salary.billdemo.dao.CodeDao;
import com.my.xl.salary.billdemo.dao.LoginDao;
import com.my.xl.salary.billdemo.pojo.SalaryBill;
import com.my.xl.salary.billdemo.pojo.SalaryCode;
import com.my.xl.salary.billdemo.pojo.SalaryLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DataService {
    @Autowired
    private LoginDao loginDao;
    @Autowired
    private BillDao billDao;
    @Autowired
    private AESToolService aesService;
    @Autowired
    private DingDingService dd;
    @Autowired
    private CodeDao codeDao;

    //用户登录工资条
    public boolean compareLoginUser(String userId, String pwd){
        if(userId==null||userId.equals("")||pwd==null||pwd.equals("")){
            System.out.println("userId或者pwd为空!!!!!!!");
            return false;
        }
        //根据userId查询数据库的密码
        SalaryLogin login=loginDao.checkUserByUserId(userId);
        String aes_pwd="";
        aes_pwd = aesService.base64Encode(pwd);//加密后的密码
        System.out.println(aes_pwd+"<====加密后的密码");
        if(login!=null){
            if(login.getPwd().equals(aes_pwd)){
                return true;
            }
        }
        return false;
    }

    //用户注册账号
    public boolean registerAcccount(JSONObject map) {
        String pwd = map.getString("pwd");
        String userId = map.getString("userId");
        String code = map.getString("code");//前端传输过来的短信验证码
        String access_token = dd.getAccess_token();
        OapiUserGetResponse info = dd.getEmpInfoByUserId(userId, access_token);
        String tele=info.getMobile();
        SalaryCode sa_code= codeDao.isCode(userId);
        boolean res_=false; //只有验证通过并且数据保存到数据库才能为true;
        if(sa_code!=null){
            String code2 = sa_code.getCode();
            if(code!=null&&!code.equals("")&&code.equals(code2)){   //验证通过之后保存数据到数据库
                System.out.println("验证通过!!!"+userId);
                SalaryLogin s = new SalaryLogin();
                s.setUserid(userId);
                s.setComment(1);
                String aes_pwd =null;
                String aes_tele =null;
                if(pwd!=null){
                    aes_pwd=aesService.base64Encode(pwd);
                }
                if (tele!=null){
                    aes_tele=aesService.base64Encode(tele);
                }
                if(aes_pwd!=null){
                    s.setPwd(aes_pwd);
                }
                if(aes_tele!=null){
                    s.setPhone(aes_tele);
                }
                boolean b=loginDao.addSakaryLogin(s);
                if(b){
                    res_=true;
                }
            }else{
                System.out.println("验证码未通过,注册失败,请重新请求!!!!!");
            }
        }
        return res_;
    }

    //修改密码
    public boolean updatePwd(JSONObject map) {
        String new_pwd = map.getString("pwd");//新密码
        String userId = map.getString("userId");
        String code = map.getString("code");//前端传输过来的短信验证码
        boolean res_=false; //只有验证通过并且数据修改成功才能为true;
        if(userId==null){
            System.out.println("传输的用户userId为空!!!!");
            return false;
        }
        SalaryCode sa_code= codeDao.isCode(userId);
        String aes_pwd =null;
        if (new_pwd!=null){
            aes_pwd=aesService.base64Encode(new_pwd);
        }
        if(sa_code!=null){
            String code2 = sa_code.getCode();
            if(code!=null&&!code.equals("")&&code.equals(code2)){  //验证通过之后修改密码
                SalaryLogin s = new SalaryLogin();
                s.setUserid(userId);
                s.setPwd(aes_pwd);
                System.out.println("验证通过,修改账号!!!"+userId);
                boolean b2 = loginDao.updateLoginPwd(s);
                if(b2){
                    res_=true;
                }
            }else{
                System.out.println("验证码未通过,修改密码失败!!!!!");
            }
        }
        return res_;
    }

    //查询工资
    public SalaryBill checkEmpSalary(String userId, String year, String month) {
        SalaryBill bill = new SalaryBill();
        Map map = new HashMap<>();
        map.put("userId",userId);
        map.put("year",year);
        map.put("month",month);
        SalaryBill bill_= billDao.checkSalary(map);//没有解密的数据
        if(bill_!=null){
            bill.setId(bill_.getId());
            bill.setComment(bill_.getComment());
            bill.setUserid(bill_.getUserid());
            bill.setUsername(bill_.getUsername());
            bill.setYear(bill_.getYear());
            bill.setMonth(bill_.getMonth());
            bill.setDate(bill_.getDate());
            bill.setDate2(bill_.getDate2());
            //解密数据
            String basic_sa = aesService.base64Decode(bill_.getBasicsalary());//基本工资
            bill.setBasicsalary(basic_sa);
            String compro_sa = aesService.base64Decode(bill_.getComprohensivesubsidy());//综合贴
            bill.setComprohensivesubsidy(compro_sa);
            String post_sa = aesService.base64Decode(bill_.getPostsalary());//岗位帖
            bill.setPostsalary(post_sa);
            String reward_sa = aesService.base64Decode(bill_.getReward());//奖金
            bill.setReward(reward_sa);
            String leave_sa = aesService.base64Decode(bill_.getLeavesalary());//病事假
            bill.setLeavesalary(leave_sa);
            String per_tax_sa = aesService.base64Decode(bill_.getPersonaltax());//个税
            bill.setPersonaltax(per_tax_sa);
            String social_sa = aesService.base64Decode(bill_.getSocialsecurity());//社保
            bill.setSocialsecurity(social_sa);
            String pre_tax_sa = aesService.base64Decode(bill_.getPretaxdeduction());//税前抵扣
            bill.setPretaxdeduction(pre_tax_sa);
            String pubserver_sa = aesService.base64Decode(bill_.getPubreservefunds());//公积金
            bill.setPubreservefunds(pubserver_sa);
            //应发和实发工资
            String actual_sa = aesService.base64Decode(bill_.getActualsalary());//实发工资
            bill.setActualsalary(actual_sa);
            String gross_sa = aesService.base64Decode(bill_.getGrosssalary());//应发工资
            bill.setGrosssalary(gross_sa);
        }else{
            System.out.println(userId+"员工"+year+"年"+month+"月份,暂时没有工资可以查询!!!");
        }
        System.out.println(JSON.toJSONString(bill)+"<===checkEmpSalary===解密后的数据");
        return bill;
    }

    //查询需要导出的数据并解密数据
    public List<SalaryBill> getExportData(ArrayList<String> monthArr, String year, String userId) {
        List<SalaryBill> dataList=billDao.checkExportData(userId,year,monthArr);
        //数据解密
        List<SalaryBill> crpList = new ArrayList<>();
        if(dataList.size()>0){
            int num=1;
            for (SalaryBill b:dataList) {
                if(monthArr.contains(b.getMonth())){
                    String basic_sa = null;//基本工资
                    String compro_sa = null;//综合贴
                    String post_sa = null;//岗位帖
                    String reward_sa = null;//奖金
                    String leave_sa = null;//病事假
                    String per_tax_sa = null;//个税
                    String social_sa = null;//社保
                    String pre_tax_sa = null;//税前抵扣
                    String pubserver_sa = null;//公积金
                    String actual_sa = null;//实发工资
                    String gross_sa = null;//应发工资
                    if(b.getBasicsalary()!=null){
                        basic_sa = aesService.base64Decode(b.getBasicsalary());//基本工资
                    }
                    if(b.getComprohensivesubsidy()!=null){
                        compro_sa = aesService.base64Decode(b.getComprohensivesubsidy());//综合贴
                    }
                    if(b.getPostsalary()!=null){
                        post_sa = aesService.base64Decode(b.getPostsalary());//岗位帖
                    }
                    if(b.getReward()!=null){
                        reward_sa = aesService.base64Decode(b.getReward());//奖金
                    }
                    if(b.getLeavesalary()!=null){
                        leave_sa = aesService.base64Decode(b.getLeavesalary());//病事假
                    }
                    if(b.getPersonaltax()!=null){
                        per_tax_sa = aesService.base64Decode(b.getPersonaltax());//个税
                    }
                    if(b.getSocialsecurity()!=null){
                        social_sa = aesService.base64Decode(b.getSocialsecurity());//社保
                    }
                    if(b.getPretaxdeduction()!=null){
                        pre_tax_sa = aesService.base64Decode(b.getPretaxdeduction());//税前抵扣
                    }
                    if(b.getPubreservefunds()!=null){
                        pubserver_sa = aesService.base64Decode(b.getPubreservefunds());//公积金
                    }
                    if(b.getActualsalary()!=null){
                        actual_sa = aesService.base64Decode(b.getActualsalary());//实发工资
                    }
                    if(b.getGrosssalary()!=null){
                        gross_sa = aesService.base64Decode(b.getGrosssalary());//应发工资
                    }
                    SalaryBill bill = new SalaryBill();
                    bill.setId(num);
                    bill.setComment(b.getComment());
                    bill.setUserid(b.getUserid());
                    bill.setUsername(b.getUsername());
                    bill.setYear(b.getYear());
                    bill.setMonth(b.getMonth());
                    bill.setDate(b.getDate());
                    bill.setDate2(b.getDate2());
                    bill.setBasicsalary(basic_sa);
                    bill.setComprohensivesubsidy(compro_sa);
                    bill.setPostsalary(post_sa);
                    bill.setReward(reward_sa);
                    bill.setLeavesalary(leave_sa);
                    bill.setPersonaltax(per_tax_sa);
                    bill.setSocialsecurity(social_sa);
                    bill.setPretaxdeduction(pre_tax_sa);
                    bill.setPubreservefunds(pubserver_sa);
                    bill.setActualsalary(actual_sa);
                    bill.setGrosssalary(gross_sa);
                    crpList.add(bill);
                    bill=null;
                    num++;
                }
            }
        }
        return crpList;
    }

    //判断用户是否登录过应用
    public boolean isLogin(String userid) {
        SalaryLogin login=loginDao.checkUserByUserId(userid);
        boolean b=false;
        if(login!=null){
           b=true;
        }
       return b;
    }

    //保存随机验证码
    public boolean saveCode(SalaryCode s) {
       SalaryCode sa_code= codeDao.isCode(s.getUserid());
       boolean b=false;
       if (sa_code==null){
           System.out.println("添加验证码!!!"+s.getUserid());
           b= codeDao.add(s);
       }else{
           System.out.println("修改验证码!!!!"+s.getUserid());
           b=codeDao.update(s);
       }
        return b;
    }

    //判断用户是否登陆过应用
    public List<String> isLoginByUser() {
      return loginDao.isLoginByUser();
    }


}
